package com.zw.pdm.module.cost.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.cost.CosSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.cost.service.CostService;
import com.zw.pdm.module.report.model.Project;
import com.zw.pdm.module.report.service.SheetService;
import com.zw.pdm.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;
import static com.zw.pdm.constant.Constant.GROUP_WEIGHTTYPE;

/**
 * @Description: 工艺成本业务
 * <p>
 * 权限：
 * dsn:proTasks:view 任务项编辑查看
 * dsn:proTasks:edit 编辑、保存
 * dsn:proTaskinfor:view 任务项查询
 * @Author: China.wsg
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/craft/cost")
@SuppressWarnings("all")
public class CraCostQueryController extends BaseController {

    @Autowired
    private CostService costService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;

    /**
     * 页面跳转
     *
     * @param model
     * @return
     */
    @GetMapping("/query")
    @RequiresPermissions({"cost:Planquery:view"})
    public String submit(Model model){

        return "cost/costPlanQuery/queryPlanPage";
    }

    /**
     * 列表查询
     */
    @ResponseBody
    @PostMapping("/list")
//    @RequiresPermissions({"report:query:view"})
    public JsonResult list(CosSheet cosSheet) {
        startPage();
        //综合查询所有
        List<CosSheet> list = costService.queryCossheet(cosSheet);
        return jsonResult(list);
    }

    /**
     * 铆焊查询、提交、修改页面
     *
     * @param model
     * @return
     */
    @GetMapping("/queryDetail/{id}")
    public String queryDetail(@PathVariable("id") Integer id, Model model) {
        CosSheet cosSheet = costService.getCosSheetById(id);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("proPart", sheetService.findPartByProjectCode(cosSheet.getProCode(), "外观".equals(cosSheet.getSpecial()) ? null : cosSheet.getSpecial()));
        if (cosSheet.getCostType() == 1) {
            //判断为设计工时确认单跳转查询
            model.addAttribute("weightType", dictCommonService.findDictByGroupName(GROUP_WEIGHTTYPE));
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'人工工时'"));
            model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'结构总重','结构净重'"));
            return "cost/dsnCost/viewCostPage";
        } else if (cosSheet.getCostType() == 2) {
            //判断为下料工时计划单跳转查询
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'人工工时'"));
            model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'主要辅材'"));
            return "cost/blanking/blankingQueryDetail";

        } else if (cosSheet.getCostType() == 3) {
            //判断为铆焊工时计划单跳转查询
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'人工工时'"));
            model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'主要辅材'"));
            return "cost/solder/solderQueryDetail";

        } else if (cosSheet.getCostType() == 4) {
            //判断为热处理计划单跳转查询
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'热处理'"));
            return "cost/heat/heatQueryDetail";

        } else if (cosSheet.getCostType() == 5) {
            //判断为装配工时计划单跳转查询
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'人工工时'"));
            model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'主要辅材'"));
            return "cost/assembling/assemblingQueryDetail";

        } else if (cosSheet.getCostType() == 6) {
            //判断为涂装工时计划单跳转查询
            model.addAttribute("hpCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'人工工时喷'"));
            model.addAttribute("wpCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'主要辅材喷'"));
            model.addAttribute("htCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'人工工时涂'"));
            model.addAttribute("wtCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'主要辅材涂'"));
            return "cost/paint/paintQueryDetail";

        } else if (cosSheet.getCostType() == 7) {
            //判断为安装工时计划单跳转查询
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'人工工时'"));
            model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id,"'主要辅材'"));
            return "cost/install/installQueryDetail";

        } else if (cosSheet.getCostType() == 8) {
            //判断为调试工时确认单跳转查询
            model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'调试工时'"));
            return "cost/dsnDebug/viewCostPage";

        } else if (cosSheet.getCostType() == 9) {
            //判断为机加工时计划单跳转查询
            model.addAttribute("cosSheet", cosSheet);
            model.addAttribute("cosMachiningDetail", costService.findMachiningCosDetailByCosSheetIdAndDataType(id, "'机加工时'"));
            return "cost/machining/machiningQueryDetail";
        }else {
            return  "";
        }

    }
}
